﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class FrmNoManage : Form
    {
        XGWMS.BLL.BLL_M006_GOODS_GROUP bll_M006_GOODS_GROUP = new XiaoGang.Soft.XGWMS.BLL.BLL_M006_GOODS_GROUP ();
        private const string dataName = "条码";

        public FrmNoManage ( string GoodsID )
        {
            InitializeComponent ();
            this.lbGoodID.Text = GoodsID;
        }

        #region 方法

        protected void InitProcess ( )
        {
            ClearControl ();
            SetTabIndex ();
            SetEnable ();
            InitialGridView ();
        }

        private void SetEnable ( )
        {

        }

        private void SetTabIndex ( )
        {
            int intTabIndex = 0; intTabIndex++;
            txtGooDsNO.TabIndex = intTabIndex; intTabIndex++;
            btnAdd.TabIndex = intTabIndex; intTabIndex++;
            btnClose.TabIndex = intTabIndex; intTabIndex++;
        }

        private void ClearControl ( )
        {

        }

        protected void InitialGridView ( )
        {
            GridMain.Width = 315;
            GridMain.Height = 143;
            GridMain.ReadOnly = true;
            GridMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            GridMain.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            GridMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            GridMain.ScrollBars = ScrollBars.Both;
            GridMain.ColumnHeadersHeight = 20;
            GridMain.ColumnHeadersVisible = true;
            GridMain.AllowUserToResizeColumns = true;
            GridMain.ColumnHeadersDefaultCellStyle.Alignment =
                DataGridViewContentAlignment.MiddleCenter;

            GridMain.Columns.Add ("NO.", "NO.");
            GridMain.Columns.Add ("ID", "");
            GridMain.Columns.Add ("GOODS_ID", "商品编号");
            GridMain.Columns.Add ("GOODS_NO", "商品条码");
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn ();
            btnDelete.Text = "删除";
            btnDelete.Width = 50;
            btnDelete.HeaderText = "删除";
            GridMain.Columns.Add (btnDelete);

            GridMain.Columns["NO."].Width = 30;
            GridMain.Columns["NO."].Frozen = true;
            GridMain.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            GridMain.Columns["NO."].Resizable = DataGridViewTriState.False;
            GridMain.Columns["NO."].SortMode = DataGridViewColumnSortMode.NotSortable;
            GridMain.Columns["ID"].Visible = false;
            GridMain.Columns["GOODS_ID"].Width = 111;
            GridMain.Columns["GOODS_ID"].Frozen = true;
            GridMain.Columns["GOODS_ID"].Resizable = DataGridViewTriState.False;
            GridMain.Columns["GOODS_ID"].SortMode = DataGridViewColumnSortMode.NotSortable;

            GridMain.Columns["GOODS_NO"].Width = 120;
            GridMain.Columns["GOODS_NO"].Resizable = DataGridViewTriState.False;
            GridMain.Columns["GOODS_NO"].SortMode = DataGridViewColumnSortMode.NotSortable;

            GridMain.AllowUserToAddRows = false;
            GridMain.AllowUserToDeleteRows = false;
            GridMain.AllowUserToResizeRows = false;
            GridMain.RowHeadersVisible = false;
            GridMain.RowTemplate.Height = 20;
            GridMain.RowTemplate.ReadOnly = true;
            GridMain.RowTemplate.Resizable = DataGridViewTriState.False;
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckProcess</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日	:</b>2010/08/18</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>	
        /// <param name="strConstrolName">控件名称</param>
        /// <returns></returns>
        protected bool CheckProcess ( string strConstrolName )
        {
            string strValue = string.Empty;
            try
            {
                switch (strConstrolName)
                {
                    case "txtGooDsNO":
                        strValue = this.txtGooDsNO.Text.Trim ();
                        if (!Method.CheckIsNumber (strValue, txtGooDsNO.MaxLength,false,true))
                        {
                            Method.MessageShow (GlobalVal.glostrMsg,dataName);
                            txtGooDsNO.Focus ();
                            return false;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteClientErrLog4Net (ex);
                return false;
            }
            return true;
        }

        /// <summary>
        /// <p><b>方法名：</b>CheckProcessLoop</p>	
        /// <p><b>概要説明:</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2010/08/18</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>	
        /// <param name="operation">Check状态枚举</param>
        /// <returns></returns>
        protected bool CheckProcessLoop ( enumOperation emOperation )
        {
            switch (emOperation)
            {
                case enumOperation.Add:
                    if (!CheckProcess ("txtGooDsNO")) return false;
                    break;
                case enumOperation.Update:
                    break;
                case enumOperation.Search:
                    break;
                case enumOperation.Delete:
                    break;
                default:
                    break;
            }
            return true;
        }

        /// <summary>
        /// 绑定grid数据
        /// </summary>
        /// <param name="GoodID"></param>
        private void ShowGrid ( string GoodID )
        {
            ReturnValue rvGoodsGroup = bll_M006_GOODS_GROUP.SearchGoodsNoProcess (GoodID);
            if (rvGoodsGroup.IsSuccess == false)
            {
                Method.MessageShow(rvGoodsGroup.ErrorID,dataName);
                return;
            }
            if (0 == rvGoodsGroup.ResultDataSet.Tables[0].Rows.Count)
            {
                GridMain.Rows.Clear ();
                return;
            }
            GridMain.Rows.Clear ();
            for (int i = 0; i < rvGoodsGroup.ResultDataSet.Tables[0].Rows.Count; i++)
            {
                GridMain.Rows.Insert (GridMain.Rows.Count,
                    (i + 1).ToString (),
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["ID"].ToString ().Trim (),
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["GoodsID"].ToString ().Trim (),
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["GoodsNo"].ToString ().Trim (),
                    "删除");                
            }
            if (GridMain.Rows.Count > 6)
            {
                GridMain.Columns["GOODS_ID"].Width = 94;
            }
            else
            {
                GridMain.Columns["GOODS_ID"].Width = 111;
            }
        }

        #endregion

        #region 事件

        private void btnClose_Click ( object sender, EventArgs e )
        {
            this.Close ();
        }
        private void btnAdd_Click ( object sender, EventArgs e )
        {
            Cursor.Current = Cursors.WaitCursor;
            if (!CheckProcessLoop (enumOperation.Add))
            {
                this.Cursor = Cursors.Default;
                return;
            }
            ReturnValue rValue = bll_M006_GOODS_GROUP.InsertGoodsNoProcess (lbGoodID.Text, txtGooDsNO.Text);
            if (rValue.IsSuccess)
            {
                ShowGrid (lbGoodID.Text);
                txtGooDsNO.Text = "";
                txtGooDsNO.Focus ();
            }
            else
            {
                Method.MessageShow (rValue.ErrorID,dataName);
                txtGooDsNO.Focus ();
            }
        }

        private void GridMain_CellClick ( object sender, DataGridViewCellEventArgs e )
        {
            int intColumnIndex = e.ColumnIndex;
            if (intColumnIndex == 4 && e.RowIndex != -1)
            {
                if (Method.MessageShow ("Q22007","") == DialogResult.Yes)
                {
                    ReturnValue rValue = bll_M006_GOODS_GROUP.DeleteGoodsNoProcess (GridMain.CurrentRow.Cells["ID"].Value.ToString ().Trim ());
                    if (rValue.IsSuccess)
                    {
                        ShowGrid (lbGoodID.Text);
                    }
                    else
                    {
                        Method.MessageShow (rValue.ErrorID,dataName);
                        txtGooDsNO.Focus ();
                    }
                }
            }
        }

        private void FrmNoManage_KeyUp ( object sender, KeyEventArgs e )
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    btnAdd_Click (sender, e);
                    break;
                case Keys.Escape:
                    btnClose_Click (sender, e);
                    break;
            }
        }

        private void FrmNoManage_Load ( object sender, EventArgs e )
        {
            InitProcess ();
            ShowGrid (lbGoodID.Text);
            txtGooDsNO.Focus ();
        }
        #endregion
    }
}